﻿using Sunny.UI;
using System;
using System.Web.Script.Serialization;
using System.Windows.Forms;

namespace MesBSW.Tools
{

    public static class SysTools
    {

        public static string errMsg { get; set; }
        public static int errCode { get; set; }


        public static Decimal DataToNum(object data)
        {
            decimal num = 0;
            if (data != null && data.ToString().Trim().Length > 0)
            {
                if (Decimal.TryParse(data.ToString().Trim(), out num) == true)
                {
                    return num;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return 0;
            }
        }



        public static void errMessage(int code,string msg) 
        {
            errCode = code;
            errMsg = msg;
        }

        public static void ShowTips(UIForm uIForm) 
        {
            if (errCode > 0)
            {
                uIForm.ShowInfoTip(errMsg);
            }
            else 
            {
                uIForm.ShowErrorTip(errMsg);
            }
        }

        public static void ShowTips(UIPage uIPage)
        {
            if (errCode > 0)
            {
                uIPage.ShowInfoTip(errMsg);
            }
            else
            {
                uIPage.ShowErrorTip(errMsg);
            }
        }



        public static string objectToJson(object obj) 
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string json = serializer.Serialize(obj);

            return json;
        }

        
        /// <summary>
        /// 设置系统输入法
        /// </summary>
        /// <param name="cultureType">语言项，如zh-CN，en-US</param>
        public static void InitializeInputLanguage(string cultureType)
        {
            //获取系统中已经安装的文字输入法
            InputLanguageCollection MyInputs = InputLanguage.InstalledInputLanguages;
            //遍历获取英文输入法索引值

            for (int i = 0; i < MyInputs.Count; i++)
            {
                if (MyInputs[i].Culture.IetfLanguageTag == cultureType)
                {
                    //获取英文输入法
                    InputLanguage enInput = InputLanguage.InstalledInputLanguages[i];
                    //设置为当前输入法
                    InputLanguage.CurrentInputLanguage = enInput;
                    break;
                }
            }
        }

    }


}
